package com.qianyu.util;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@RequiredArgsConstructor
public class CacheUtil {

    public final RedisUtil redisUtil;

    private static CacheUtil cacheUtil;

    private void init() {
        cacheUtil = this;
    }

    /**
     * 添加版本控制超时集合key
     * @param orderSn
     * @param time
     */
    public static void addVersionConfSortSet(Long orderSn, Integer time) {
        cacheUtil.redisUtil.zAdd(CacheKeyUtil.versionConfSortedSet(), orderSn, time);
    }

    /**
     * 添加QQ到超时集合
     * @param qq
     * @param time
     */
    public static void addAccountTimeoutSortSet(String qq, Integer time) {
        cacheUtil.redisUtil.zAdd(CacheKeyUtil.accountTimeoutSortSet(), qq, time);
    }

    /**
     * 移除订单超时集合中的订单
     * @param orderSn
     * @return
     */
    public static Long delOrderTimeoutSortSet(Long orderSn) {
        return cacheUtil.redisUtil.zRemove(CacheKeyUtil.versionConfSortedSet(), orderSn);
    }

    /**
     * 获取订单超时集合信息
     * @param time
     * @param offset
     * @param count
     * @return
     */
    public static Set<String> getOrderTimeoutSortSet(Integer time, long offset, long count) {
        return cacheUtil.redisUtil.zRangeByScore(CacheKeyUtil.versionConfSortedSet(), 0, time, offset, count);
    }

}
